package com.shierke.Ume.ui.adapter.explore

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.shierke.Ume.R
import com.shierke.Ume.util.click
import kotlinx.android.synthetic.main.item_explore_report_reason.view.*

/**
 * Create by Ybx
 * on 2020-10-18
 * Desribe:
 */
class ExploreReportReasonAdapter(var datas: ArrayList<String>) :
    RecyclerView.Adapter<ExploreReportReasonAdapter.ViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {

        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_explore_report_reason, null, false)
        return ViewHolder(view)

    }

    override fun getItemCount() = datas.size

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.onBind(position, datas)
    }


    private var chooseIndex = -1

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        fun onBind(position: Int, datas: ArrayList<String>) {

            with(itemView) {


                rootView.click {
                    chooseIndex = position
                    notifyDataSetChanged()
                }

                if (chooseIndex == position) {
                    imageChoose.setBackgroundResource(R.mipmap.explore_report_reason_choose)
                } else {
                    imageChoose.setBackgroundResource(R.mipmap.explore_report_reason_no_choose)
                }

                content.text = datas[position]
            }

        }


    }
}